package com.blue.controller.author;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.author.UserPostPO;
import com.blue.dto.author.UserPostDTO;
import com.blue.service.author.IUserPostService;
import com.blue.common.entity.ResponseResult;

import java.util.List;


/**
 * 用户岗位关联表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-02-15
 */
@RestController
@RequestMapping("/author/userPost")
@Tag(name = "用户岗位关联表")
public class UserPostController {
    @Autowired
    private IUserPostService userPostService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(UserPostDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        UserPostPO userPostPO = ConvertUtils.convert(UserPostPO.class, requestDTO);
        MPJQueryWrapper<UserPostPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(userPostPO);
        Page<UserPostPO> page = new Page<>(pageNum, pageSize);
        Page<UserPostPO> pageResult = userPostService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody UserPostDTO requestDTO) {
        UserPostPO userPostPO = ConvertUtils.convert(UserPostPO.class, requestDTO);
        userPostService.save(userPostPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody UserPostDTO requestDTO) {
        UserPostPO userPostPO = ConvertUtils.convert(UserPostPO.class, requestDTO);
        userPostService.updateById(userPostPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        userPostService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody UserPostDTO requestDTO) {
        UserPostPO userPostPO = userPostService.getById(requestDTO.getId());
        return ResponseResult.okResult(userPostPO);
    }
}
